package com.demo2do.fms.scheduler.scheduler;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.demo2do.fms.scheduler.entity.Branch;
import com.demo2do.fms.scheduler.entity.Vendor;
import com.demo2do.fms.scheduler.service.BranchReader;
import com.demo2do.fms.scheduler.service.BranchWriter;
import com.demo2do.fms.scheduler.service.VendorReader;
import com.demo2do.fms.scheduler.service.VendorWriter;

/**
 * 
 * @author Ysy
 *
 */
@Component("eppScheduler")
public class EppScheduler {
    
    private static final Logger logger = LoggerFactory.getLogger(EppScheduler.class);
    
    @Autowired
    private BranchReader branchReader;
    
    @Autowired
    private BranchWriter branchWriter;
    
    @Autowired
    private VendorReader vendorReader;
    
    @Autowired
    private VendorWriter vendorWriter;
    
    /**
     * 同步Epp分区信息至FMS
     */
    public void syncBranch() {
        
        logger.info("Starting to sync branch data from epp. ");
        
        //1 list branches of epp
        List<Branch> eppBranches = branchReader.listEppBranch();
        logger.warn("#syncBranch() - sync epp branch size=[{}]", eppBranches.size());
        
        //2 sync
        for(Branch branch : eppBranches) {
        	
            try{
                
                branchWriter.sync(branch.stateToDisabled());
                
            } catch(Exception e){
                logger.error("#syncBranch() - find error when sync branch", e);
            }
            
        }
        
        logger.info("Ending sync branch data from epp. ");
        
    }
    
    /**
     * 同步EPP一般供方至FMS
     */
    public void syncVendor() {
        
        logger.info("Starting to sync vendor data from epp. ");
        
        //1 list vendors of epp
        List<Vendor> eppVendors = vendorReader.listEppVendor();
        logger.warn("#syncVendor() - sync epp vendor size=[{}]", eppVendors.size());
        
        //2 sync
        for(Vendor vendor : eppVendors) {
        	
            try{
            	
                vendorWriter.sync(vendor);
                
            } catch(Exception e){
                logger.error("#syncVendor() - find error when sync vendor", e);
            }
            
        }
        
        logger.info("Ending to sync vendor data from epp. ");
        
    }

}
